//获取初始数据，将数据存入内存
if(localStorage.getItem('key')==null){
    $.ajax({
        url:'../../api/data-teacher.json',
        dataType:'json',
        success:function (data){
            let  jsonData=JSON.stringify(data.data)//转换为字符串
            localStorage.setItem('key',jsonData)
            location.reload()
            // let lengthT=data.data.length
        }
    })
}
let teacherD=JSON.parse(localStorage.getItem('key'));//将数据放入内存
// console.log(teacherD);
// console.log(teacherD[0].level);
//预览表格形成
layui.use('table', function(){
    let $ = layui.jquery;
    let table = layui.table;
    let layer = layui.layer;
    table.render({
        elem: '#role-show',
        data:teacherD,
        // url: '../../api/data-teacher.json',
        page:true,//开启分页
        id:'qx-table',
        limit:9,//限制显示行数
        height: 483,
        cols: [[
            {field:'aa', width:60,type:"numbers", title: '序号',align:'center'}
            ,{field:'复选框', width:60,type:"checkbox", title: '选择',align:'center'}
            ,{field:'id', width:60, title: 'ID', sort: true,align:'center'}
            ,{field:'userName', width:120, title: '姓名',align:'center'}
            ,{field:'position', width:120, title: '职位', sort: true,align:'center'}
            ,{field:'sex', width:80, title: '性别',align:'center'}
            ,{field:'Email', title: '邮箱', width: '200', minWidth: 100,align:'center'} //minWidth：局部定义当前单元格的最小宽度
            ,{field:'emergency',width:120, title: '联系电话',align:'center'}
            ,{field:'level', title: '权限等级',width:100,style:'background-color: #01AAED; color: #fff;',align:'center'}
            ,{field:'操作其权限',width:150, title: '修改权限等级',toolbar:'#px-role-set'}
        ]],
        toolbar: "#qx-set-more",

    });
    //批量设置职位
    table.on('toolbar(text)',function (obj){
        // console.log(obj)
        let objInformation=table.checkStatus(obj.config.id)//获取选中对象的信息
        console.log(objInformation)
        let eventName=obj.event;
        if(eventName=="getCheckData"){
            // layer.alert("点击了批量操作")
            layer.open({
                type: 1,
                title: "选中用户",
                area: ['50%', '30%'],
                btn: ['确定', '取消'],
                content:objInformation,
                yes: function (index) {
                    layer.close
                }
            })
        }
    })
    //监听行工具栏事件——删除
    //监听行工具事件
    table.on('tool(text)', function(obj){
        // let teacherD=JSON.parse(localStorage.getItem('teacher'));
        // console.log(teacherD.length)
        let tr = obj.data;
        console.log(obj)
        if(obj.event === 'del'){//删除操作
            layer.confirm('确定删除该人员吗？', function(index){
                for (let i=0;i<teacherD.length;i++){
                    if (teacherD[i].id==obj.data.id){
                        teacherD.splice(i,1,)
                        let  jsonData=JSON.stringify(teacherD)//转换为字符串
                        localStorage.setItem('key',jsonData)
                        teacherD=JSON.parse(localStorage.getItem('key'));
                        layer.alert('删除成功！')
                        location.reload()
                    }
                }
                layer.close(index);
            });
        } else if(obj.event === 'edit'){//编辑操作

            layer.open({
                type: 1,
                title:"选择职位等级",
                area:['50%','50%'],
                btn: ['确定', '取消'],
                shadeClose: true,
                content: $("#qx-chose"),
                yes:function(index){
                    let options=$("#qx-set-select option:selected");
                    if (options.val()=="园长"){
                        // console.log(options.val())
                        layer.open({
                                type: 1
                                ,title: "友情提示" //不显示标题栏
                                ,closeBtn: false
                                ,area: '300px;'
                                ,shade: 0.8
                                ,id: 'LAY_yz' //设定一个id，防止重复弹出
                                ,btn: ['确认', '返回']
                                ,btnAlign: 'c'
                                ,moveType: 1 //拖拽模式，0或者1
                                ,content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">是否授予园长权限？，此权限是唯一的哟</div>',
                                yes:function (index){
                                    let a=true;
                                    for (let i=0;i<teacherD.length;i++){
                                        if(teacherD[i].level=="园长"){
                                            // console.log(1);
                                            layer.alert('已有园长职位，请先将原园长职位撤销')
                                            a=false
                                        }
                                    }
                                    for (let i=0;i<teacherD.length;i++){
                                        if (a){
                                            if (teacherD[i].id==obj.data.id){
                                                console.log(i);
                                                obj.data.level="园长"
                                                // console.log(obj.data);
                                                teacherD.splice(i,1,obj.data)
                                                let  jsonData=JSON.stringify(teacherD)//转换为字符串
                                                localStorage.setItem('key',jsonData)
                                                teacherD=JSON.parse(localStorage.getItem('key'));
                                                location.reload()
                                                layer.alert('设置成功，已授予园长权限')
                                            }
                                        }
                                    }
                                    // layer.close(index);
                                }
                                ,success: function(){

                                }
                            });
                    }
                    if (options.val()=="管理员"){
                        layer.open({
                            type: 1
                            ,title: "友情提示" //不显示标题栏
                            ,closeBtn: false
                            ,area: '300px;'
                            ,shade: 0.8
                            ,id: 'LAY_gly' //设定一个id，防止重复弹出
                            ,btn: ['确认', '返回']
                            ,btnAlign: 'c'
                            ,moveType: 1 //拖拽模式，0或者1
                            ,content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">是否授予管理员权限？，此权限是唯一的哟</div>',
                            yes:function (){
                                let b=true;
                                for (let i=0;i<teacherD.length;i++){
                                    if(teacherD[i].level=="管理员"){
                                        // console.log(1);
                                        layer.alert('已有管理员职位，请先将原管理员职位撤销')
                                        b=false
                                    }
                                }
                                for (let i=0;i<teacherD.length;i++){
                                    if (b){
                                        if (teacherD[i].id==obj.data.id){
                                            console.log(i);
                                            obj.data.level="管理员"
                                            // console.log(obj.data);
                                            teacherD.splice(i,1,obj.data)
                                            let  jsonData=JSON.stringify(teacherD)//转换为字符串
                                            localStorage.setItem('key',jsonData)
                                            teacherD=JSON.parse(localStorage.getItem('key'));
                                            location.reload()
                                            layer.alert('设置成功，已授予管理员权限')
                                        }
                                    }
                                }
                            }
                            ,success: function(layero){
                            }
                        });
                    }

                    if (options.val()=="教师"){
                        for (let i=0;i<teacherD.length;i++){
                                // layer.alert('已有园长职位，请先将原园长职位撤销')
                                // console.log(obj.data);
                                if (teacherD[i].id==obj.data.id){
                                    obj.data.level="教师"
                                    console.log(1);
                                    teacherD.splice(i,1,obj.data)
                                    let  jsonData=JSON.stringify(teacherD)//转换为字符串
                                    localStorage.setItem('key',jsonData)
                                    teacherD=JSON.parse(localStorage.getItem('key'));
                                    location.reload()
                                    layer.alert('设置成功，已授予教师权限')
                                    break
                                }
                        }
                        // obj.data.level="教师"
                        //
                        // console.log(obj.data);
                        // teacherD.splice(1,1,obj.data)
                        // let  jsonData=JSON.stringify(teacherD)//转换为字符串
                        // localStorage.setItem('teacher',jsonData)
                        // localStorage.setItem()

                    }

                    // console.log(options.val())
                    layer.close(index);
                    $('#qx-set-select').children().eq(0).attr("selected","");
                },
                success:function (index){
                    $('#username').val(tr.userName)
                    // $("#qx-set-select option:selected").val(tr.level)

                },
            });
        }
    });
    //搜索职位按钮
    $(document).on('click','#qx-set',function (data){
        let userLevel=$('#qx-select').val()
        for (let i=0;i<teacherD.length;i++){
            if (teacherD[i].level==userLevel){
                layer.open({
                    type: 0,
                    title: '查询到的值',
                    content: '用户的ID为:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+teacherD[i].id+
                        '<br/>用户的姓名为:&nbsp;&nbsp;&nbsp;&nbsp;'+teacherD[i].userName+
                        '<br/>用户的权限等级为:&nbsp;&nbsp;&nbsp;'+teacherD[i].level,
                    skin: 'demo-class',
                    area: ['500px', '300px']
                })
                    break;
            }
        }

    })
    //搜索姓名
    $(document).on('click','#qx-research',function (data){
        let username=$('#nameSearch').val()
        let c=true
        // console.log(username)
        for (let i=0;i<teacherD.length;i++){
            if (teacherD[i].userName==username){
                layer.open({
                    type: 0,
                    title: '查询到的值',
                    content: '用户的ID为:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+teacherD[i].id+
                        '<br/>用户的姓名为:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+teacherD[i].userName+
                        '<br/>用户的权限等级为:&nbsp;&nbsp;&nbsp;'+teacherD[i].level,
                    skin: 'demo-class',
                    area: ['500px', '300px']
                })
                break;
                c=false
            }else{

                    layer.open({
                        type: 0,
                        title: '未查询到的值<i class="layui-icon layui-icon-heart-fill" style="font-size: 30px; color: #fa0429;"></i>',
                        content:'<i class="layui-icon layui-icon-face-cry" style="font-size: 50px; color: #FF5722;"></i> 抱歉找不到该用户',
                        skin: 'demo-class',
                        area: ['500px', '300px']
                    })


            }

        }

    })


});


//提交按钮监听
layui.use(['form', 'layedit', 'laydate'], function(){
    //sj-管理员
    let form = layui.form;
    form.on('submit(qx-sj-form1)', function(data){
        layer.alert(JSON.stringify(data.field), {
            title: '提交的数据权限信息-管理员',
            area:['40%','25%'],
        })
        return false;
    });
    //sj-园长
    form.on('submit(qx-sj-form2)', function(data){
        layer.alert(JSON.stringify(data.field), {
            title: '提交的数据权限信息-园长',
            area:['40%','25%'],
        })
        return false;
    });
    //sj-教师
    form.on('submit(qx-sj-form3)', function(data){
        layer.alert(JSON.stringify(data.field), {
            title: '提交的数据权限信息-教师',
            area:['40%','25%'],
        })
        return false;
    });
    //gn-管理员
    form.on('submit(qx-gn-form1)', function(data){
        layer.alert(JSON.stringify(data.field), {
            title: '提交的功能权限信息-管理员',
            area:['40%','25%'],
        })
        return false;
    });
    //gn-园长
    form.on('submit(qx-gn-form2)', function(data){
        layer.alert(JSON.stringify(data.field), {
            title: '提交的功能权限信息-园长',
            area:['40%','25%'],
        })
        return false;
    });
    //gn-教师
    form.on('submit(qx-gn-form3)', function(data){
        layer.alert(JSON.stringify(data.field), {
            title: '提交的功能权限信息-教师',
            area:['40%','25%'],
        })
        return false;
    });
})


//公告数据查看——仅班级出现
// console.log($('input:radio[name="get-range1"]'))
// console.log($("#radio2")[0].title)
let chooseClass=function (){
    $('.classResearch').css("display","block")
}
// console.log($("#radio2").next())
$("#radio2").on('click',function (){
    console.log("选中了班级")
})

//修改内存数据权限
// function revise(a){
//     console.log(a);
//     let qxValue=options.val();
//     for(let i=0;i<teacherD.length;i++){
//         if (teacherD[i].level==qxValue){
//             teacherD[i].level=options.val();
//             // $('.'+myValue).remove()
//             localStorage.setItem('teacher',JSON.stringify(teacherD))
//             break;
//         }
//     }
// }

// let ggjs1=$('input:radio[name="get-range1"]')//管理员
// let ggjs2=$('input:radio[name="get-range2"]')//园长
// let ggjs3=$('input:radio[name="get-range3"]')//教师
// // console.log(ggjs.length)
// for (let i=0 ; i<ggjs1.length ; i++){
//
// }


